<template>
   	<!-- <el-button  type="primary"  @click="exportData" icon="Download">导出</el-button> -->
	<el-button v-if="!isText" type="primary" @click="exportData" icon="Download">{{showText}}</el-button>   
	<el-button v-if="isText" text type="primary" size="small" @click="exportData" >{{showText}}</el-button>
</template>
<script>
export default {
    name: "exportFile",
    props: {
        queryType: { type: String, default: "" },
        queryList: { type: String, default: "" },
        fileName: { type: String, default: "" },
		selectedKeys:{type: Array, default: () => []}, 
		isTest: { type: Boolean, default: false },
		isText: { type: Boolean, default: false },
		showText : { type: String, default: "导出" }, 
    },
    data() {
        return {
            queryParam: {},
        };
    },
    watch: {
        queryType: {
            handler(newVal,oldVal) { 
                console.log("queryType",newVal);
                //this.queryType = { ...this.queryType,...newVal }; 
            },
        }
    },
    methods: {
            //导出
			async exportData()
			{
				//无需勾选
				if(this.isTest)
				{
					try {
							
							await this.$API.auth.PostDownApi.post(this.queryType,this.queryList).then((res)=>
							{
								this.downloadfile(res.data);
							}).catch((err) => {
								this.$message.error('下载错误：获取文件流错误')
							});
						} catch (error) {
							this.loading = false;
							return false;
						}
				}else
				{
					console.log("导出的数据ID",this.selectedKeys)
					if (this.selectedKeys.length>0) {

						this.queryParam = { ...this.queryList,selectedKeys:this.selectedKeys };
						console.log("请求参数",this.queryParam)
						try {
							
							await this.$API.auth.PostDownApi.post(this.queryType,this.queryParam).then((res)=>
							{
								this.downloadfile(res.data);
							}).catch((err) => {
								this.$message.error('下载错误：获取文件流错误')
							});
						} catch (error) {
							this.loading = false;
							return false;
						}
					} else {
						this.$message.error("请选择要导出的数据");
					}
				}
				
				
			},
			downloadfile(blob) {
				var _fileName=this.fileName+".xlsx";
				let fileReader = new FileReader()
				//var blob = new Blob([res], {type: 'application/octet-stream;charset=UTF-8'})
				fileReader.onload = function () 
				{
				try {
					var downloadElement = document.createElement('a')
					var href = window.URL.createObjectURL(blob) // 创建下载的链接
					downloadElement.style.display = 'none'
					downloadElement.href = href
					downloadElement.download = decodeURI(_fileName) // 下载后文件名
					document.body.appendChild(downloadElement)
					downloadElement.click() // 点击下载
					document.body.removeChild(downloadElement) // 下载完成移除元素
					window.URL.revokeObjectURL(href)
				} catch (err) {}
				}
				fileReader.readAsText(blob)
			},
        
    },
}
</script>
<style lang="scss">
.platform-auto-complete{
    .el-autocomplete-suggestion__wrap {
        height:400px;
        margin: 10px;
    }
    

}
.rowStyle{
    width: 500px;
    border-bottom: 1px solid #ebeef5;
}

</style>